package com.dmx.platform.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

import com.dmx.fw.bean.PageInfo;
import com.dmx.fw.dao.interfaces.Dao;
import com.dmx.fw.service.impl.BaseServiceImpl;
import com.dmx.platform.bean.PriceBean;
import com.dmx.platform.dao.InformationDao;
import com.dmx.platform.entity.TBaseinfo;
import com.dmx.platform.entity.TDetailinfo;
import com.dmx.platform.entity.TImage;
import com.dmx.platform.entity.TPricePlan;
import com.dmx.platform.service.InformationService;

/**
 *@author: Beni Yang
 *@date:2012-8-18
 *@function:
 */
public class InformationServiceImpl extends BaseServiceImpl implements InformationService {
	
	private InformationDao informationDao;

	@Override
	public void batchDeleteBaseInfoByIds(String[] ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void batchDeleteDetailInfoByIds(String[] ids) {
		for(String s: ids){
			deleteDetailInfoById(s);
		}
		
	}

	@Override
	public void deleteBaseInfoById(String id) {
		this.informationDao.deleteBaseInfoById(id);
		
	}

	@Override
	public void deleteDetailInfoById(String id) {
		Integer i=Integer.parseInt(id);
		this.removeObject(TDetailinfo.class, i);
		
		this.informationDao.deletePricePlanByOwnerId(i);
		
	}

	@Override
	public TBaseinfo findBaseInfoById(String id) {
		return informationDao.findBaseInfoById(id);
		
	}

	@Override
	public TDetailinfo findDetailInfoById(String id) {
		Integer d=Integer.parseInt(id);
		return (TDetailinfo) this.getObject(TDetailinfo.class, d);
		
	}

	@Override
	public void saveOrUpdateBaseInfo(TBaseinfo baseInfo) {
		this.informationDao.saveOrUpdateBaseInfo(baseInfo);
		
	}

	@Override
	public void saveOrUpdateDetailInfo(TDetailinfo detailInfo) {
		this.informationDao.saveOrUpdateDetailInfo(detailInfo);
		
	}

	@Override
	public List<TPricePlan> findPricePlanByOwnerId(String id) {
		 return this.informationDao.findPricePlan(Integer.parseInt(id));
	}
	
	@Override
	public boolean saveOrUpdatePricePlan(List<PriceBean> priceList,Integer ownerId){
		if(!priceList.isEmpty()){
			List<TPricePlan> list=new ArrayList<TPricePlan>();
			TPricePlan plan=null;
			for(PriceBean b : priceList){
				plan=new TPricePlan();
				plan.setOwnerId(ownerId);
				plan.setPriceType(Integer.parseInt(b.getPriceType()));
				plan.setTimeSegment(Integer.parseInt(b.getTimeSegment()));
				plan.setMinBookNum(Integer.parseInt(b.getMinNum()));
				plan.setPrice(Float.parseFloat(b.getPrice()));
				
				list.add(plan);
			}
			if(this.informationDao.saveOrUpdatePricePlan(list)){
				return true;
			}
		}
		return false;
	}
	
	

	



	public InformationDao getInformationDao() {
		return informationDao;
	}

	public void setInformationDao(InformationDao informationDao) {
		this.informationDao = informationDao;
	}

	@Override
	public boolean deletePricePlanByOwnerId(Integer ownerId) {
		return this.informationDao.deletePricePlanByOwnerId(ownerId);
		
	}

	@Override
	public boolean operateDetailInfo(String id, Integer op) {
		TDetailinfo info=this.findDetailInfoById(id);
		info.setStatus(op);
		this.saveOrUpdateDetailInfo(info);
		//add others business....
		return true;
		
	}

	@Override
	public boolean deleteImage(String imageId) {
		try{
			this.removeObject(TImage.class, Integer.parseInt(imageId));
		}catch(Exception e){
			log.error("delete image failed "+e.getMessage());
			return false;
		}
		return true;
		
	}

	@Override
	public boolean saveImage(TImage image) {
		try {
			this.saveObject(image);
		} catch (Exception e) {
			log.error("save image to db error "+e.getMessage());
			return false;
		}
		return true;
		
	}

	@Override
	public TImage getImageById(String imageId) {
		List<TImage> images=this.getObjects(TImage.class, new Integer[]{Integer.parseInt(imageId)});
		if(images.isEmpty())
			return null;
		else
			return images.get(0);
		
	}


	


}
